package com.jjsc.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjsc.client.api.domain.UserAddress;
import com.jjsc.client.mapper.UserAddressMapper;
import com.jjsc.client.service.IUserAddressService;
import com.jjsc.common.core.context.SecurityContextHolder;
import com.jjsc.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-10-23
 * @Description:
 * @Version: 1.0
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress>
        implements IUserAddressService {

    /**
     * 查询用户地址列表
     *
     * @param userId 用户ID
     * @return 用户地址列表
     */
    @Override
    public List<UserAddress> selectUserAddressList(Long userId) {
        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getUserId, userId);
        return this.list(wrapper);
    }

    /**
     * 添加用户地址
     *
     * @param userAddress 用户地址
     * @return 添加结果
     */
    @Override
    public int addUserAddress(UserAddress userAddress) {
        userAddress.setUserId(SecurityContextHolder.getUserId());
        String province = userAddress.getProvinceCode(); //省
        String city = userAddress.getCityCode(); //市
        String district = userAddress.getDistrictCode(); //区
        String address = userAddress.getAddress(); //详细地址
        userAddress.setFullAddress(province + city + district + address); //完整地址
        //设置默认地址
        if (userAddress.getIsDefault() == 1) {
            LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserAddress::getUserId, userAddress.getUserId())
                    .set(UserAddress::getIsDefault, 0);
            baseMapper.update(new UserAddress(), updateWrapper);
        }
        return baseMapper.insert(userAddress);
    }

    /**
     * 修改用户地址
     *
     * @param userAddress 用户地址
     * @return 修改结果
     */
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        userAddress.setUserId(SecurityContextHolder.getUserId());
        String province = userAddress.getProvinceCode(); //省
        String city = userAddress.getCityCode(); //市
        String district = userAddress.getDistrictCode(); //区
        String address = userAddress.getAddress(); //详细地址
        userAddress.setFullAddress(province + city + district + address); //完整地址
        //设置默认地址
        if (userAddress.getIsDefault() == 1) {
            LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserAddress::getUserId, userAddress.getUserId())
                    .set(UserAddress::getIsDefault, 0);
            baseMapper.update(new UserAddress(), updateWrapper);
        }
        return baseMapper.updateById(userAddress);
    }
}
